package milkyway.hightech.formatter;

import milkyway.hightech.entry.Entry;

import java.text.DateFormat;
import java.text.NumberFormat;

import java.util.Date;
import java.util.ResourceBundle;

/**
 *
 * @author michael
 */
public class FormatterHTML implements Formatter
{
    private final int COLOR1 = 0;
    private final int COLOR2 = 1;
    private StringBuffer sb = new StringBuffer();
    private int currentColor = COLOR1;
    private ResourceBundle rb;

    @Override
    public void writeHead()
    {
        sb.append("<html>");
        sb.append("\n\t<table cellpadding='4' cellspacing='4' border='1'" +
            "style='text-align: center; vertical-align: top'>");
        sb.append("\n\t\t<thead>");
        sb.append("\n\t\t\t<tr>");
        sb.append("\n\t\t\t\t<th>");
        sb.append(rb.getString("date"));
        sb.append("</th>");
        sb.append("\n\t\t\t\t<th>");
        sb.append(rb.getString("reason"));
        sb.append("</th>");
        sb.append("\n\t\t\t\t<th>");
        sb.append(rb.getString("amount"));
        sb.append("</th>");
        sb.append("\n\t\t\t</tr>");
        sb.append("\n\t\t</thead>");
    }

    /**
     *
     * @param entry
     */
    @Override
    public void formatTo(Entry entry)
    {
        sb.append("\n\t\t");

        String Column = "<tr bgcolor=\"" + getColor() + "\">";

        sb.append(Column);
        sb.append("\n\t\t\t");
        sb.append("<td>");
        sb.append("\n\t\t\t\t");

        Date date = entry.getDate();

        sb.append(((DateFormat)rb.getObject("dateFormat")).format(date));
        sb.append("\n\t\t\t");
        sb.append("</td>");
        sb.append("\n\t\t\t");
        sb.append("<td>");
        sb.append("\n\t\t\t\t");
        sb.append(entry.getReasonOfTransfer());
        sb.append("\n\t\t\t");
        sb.append("</td>");
        sb.append("\n\t\t\t");
        sb.append("<td>");
        sb.append("\n\t\t\t\t");
        sb.append(entry.getSign());

        NumberFormat amount = ((NumberFormat)rb.getObject("numberFormat"));
        String formattetValue = amount.format(entry.getAmount());
        String s = rb.getObject("currencyPlainText").toString();
        String s2 = rb.getObject("currencyHTML").toString();

        formattetValue = formattetValue.replaceAll(s, s2);
        sb.append(formattetValue);
        sb.append("\n\t\t\t");
        sb.append("</td>");
        sb.append("\n\t\t");
        sb.append("</tr>");
        sb.append("\n");
    }

    @Override
    public void writeFoot()
    {
        sb.append("\t</table>\n");
        sb.append("</html>");
        sb.append("\n");
    }

    /**
     *
     * @return
     */
    @Override
    public String getFormattedText()
    {
        return sb.substring(0);
    }

    @Override
    public void cleanUp()
    {
        sb.delete(0, sb.capacity());
        currentColor = COLOR1;
    }

    /**
     *
     * @return
     */
    private String getColor()
    {
        switch (currentColor)
        {
            case COLOR1:
                currentColor = COLOR2;
                return "#dddddd";

            case COLOR2:
                currentColor = COLOR1;
                return "#999999";
            
            default:
                return "#ffffff";
        }
    }

    /**
     *
     * @param rb
     */
    @Override
    public void setLocale(ResourceBundle rb)
    {
        this.rb = rb;
    }

    /**
     *
     * @return
     */
    @Override
    public String getMimeType()
    {
        return ".html";
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
